//不带头双向列表
public class LinkedList {

    static class ListNode {
        private int val;
        private ListNode next;
        private ListNode pre;

        public ListNode(int data) {
            this.val = data;
        }
    }
    public  ListNode head;
    public  ListNode tail;
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if( head == null) {  //首次插入数据
            head = tail = node;
        }
        else {
            node.next = head;
            head.pre = node;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if( tail == null) {  //首次插入数据
            head = tail = node;
        }
        else {
            tail.next = node;
            node.pre = tail;
            tail = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if(!checkIndex(index)) {
            return false;
        }
        if(index == 0) {     //头插
            addFirst(data);
            return true;
        }
        if(index == size()) { //尾插
            addLast(data);
            return true;
        }
        //此时index位置在链表中
        ListNode cur = head;
        //找到index位置
        while(index-- != 0) {
            cur = cur.next;
        }
        ListNode node = new ListNode(data);
        node.next = cur;
        node.pre = cur.pre;
        cur.pre.next = node;
        cur.pre = node;
        return true;
    }

    //检查位置是否合法
    public boolean checkIndex(int index) {
        if(index <0 || index>size()) {
            return false;
        }
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            if(cur.val == key) {
                if(cur == head) {  //删除的头节点
                    head = head.next;
                    if(head != null) {  //头节点后还有其他节点
                        head.pre = null;
                    }else {    //当前只有一个节点
                        tail = null;
                    }
                }else {
                    cur.pre.next = cur.next;
                    if(cur.next != null) {  //中间节点
                        cur.next.pre = cur.pre;
                    }else {                //尾巴节点
                        tail = tail.pre;   //尾巴节点被删除，尾节点向前移动
                    }
                }
                return;
            }
            cur = curNext;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            if(cur.val == key) {
                if(cur == head) {  //删除的头节点
                    head = head.next;
                    if(head != null) {  //头节点后还有其他节点
                        head.pre = null;
                    }else {    //当前只有一个节点
                        tail = null;
                    }
                }else {
                    cur.pre.next = cur.next;
                    if(cur.next != null) {  //中间节点
                        cur.next.pre = cur.pre;
                    }else {                //尾巴节点
                        tail = tail.pre;   //尾巴节点被删除，尾节点向前移动
                    }
                }

            }
            cur = curNext;
        }
    }



    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while ( cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.pre = null;
            cur = curNext;
        }
        head = tail = null;
    }
}
